package com.se.Register;

import com.se.common.MessageType;
import com.se.common.User;
import com.se.utils.ClientUtils;
import com.se.utils.ServiceUtils;
import com.se.utils.StageManager;
import com.se.utils.StageType;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.regex.Pattern;

public class RegisterController {
    public Button btnRegister;
    public Button btnLoginJump;
    @FXML
    public Button btnClose;
    public Button btnMin;
    public TextField tfRegister;
    public PasswordField pwfRegister;
    public PasswordField pwfRegisterConfirm;

    @FXML
    protected void btnCloseClick() {
        StageManager.get(StageType.REGISTER_STAGE).close();
    }

    @FXML
    protected void btnLoginJumpOnClick() {

        Stage registerStage = StageManager.get(StageType.REGISTER_STAGE);
        if(registerStage != null) {
            registerStage.hide();
        }
        Stage loginStage = StageManager.get(StageType.LOGIN_STAGE);
        if(loginStage != null) {
            loginStage.show();
            //return;
        }else {
            // 如果没有现成的登录界面，就新建一个
            ClientUtils.showLoginStage();
        }
        //ClientUtils.showLoginStage();
    }

    @FXML
    protected void btnRegisterOnClick(){
        RegisterService rs = new RegisterService();
        String regex1 = "^\\d.*";

        if(tfRegister.getText().isEmpty()) {
            ClientUtils.information_Alert("用户名不能为空！");
            return;
        }

        if(Pattern.matches(regex1, tfRegister.getText())) {
            ClientUtils.information_Alert("用户名不能以数字开头！");
            return;
        }

        if(pwfRegister.getText().isEmpty()) {
            ClientUtils.information_Alert("密码不能为空！");
            return;
        }

        if(!pwfRegister.getText().equals(pwfRegisterConfirm.getText())) {
            ClientUtils.information_Alert("密码不一致！");
            return;
        }

        try{
            String status = rs.checkUserRegister(tfRegister.getText(), pwfRegister.getText());
            if(MessageType.MESSAGE_REGISTER_SUCCESS.equals(status)) {
                ClientUtils.information_Alert("注册成功！");

                ServiceUtils.setSpeaker(new User(tfRegister.getText(), pwfRegister.getText()));
                ClientUtils.showMainStage();
                StageManager.get(StageType.REGISTER_STAGE).close();
                StageManager.remove(StageType.REGISTER_STAGE);
                StageManager.remove(StageType.LOGIN_STAGE);
            }
            else if(MessageType.MESSAGE_REGISTER_USER_EXIST.equals(status)) {
                ClientUtils.information_Alert("该用户名已存在！");
            }
            else if(MessageType.MESSAGE_CLIENT_REQUEST_ERROR.equals(status)) {
                ClientUtils.information_Alert("您已经登录！");
            }
            else if(MessageType.MESSAGE_SERVER_ERROR.equals(status)) {
                ClientUtils.information_Alert("服务器异常！");
            }
            else {
                ClientUtils.information_Alert("未知错误！");
            }

            //后续优化异常处理
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
    @FXML
    private void btnCloseMouseEntered() {
        btnClose.setStyle("-fx-background-color: red; -fx-cursor: hand;");
    }

    @FXML
    private void btnCloseMouseExited() {
        btnClose.setStyle("-fx-background-color: transparent; -fx-cursor: hand;");
    }

    public void btnMinimizeMouseEntered(MouseEvent mouseEvent) {
        btnMin.setStyle("-fx-background-color: grey; -fx-cursor: hand;");
    }

    public void btnMinimizeMouseExited(MouseEvent mouseEvent) {
        btnMin.setStyle("-fx-background-color: transparent; -fx-cursor: hand;");
    }

    public void btnMinimizeClick(ActionEvent actionEvent) {
        Stage stage = (Stage) btnMin.getScene().getWindow();
        stage.setIconified(true);
    }
}
